//
//  DateTools.swift
//  ShouChong
//
//  Created by yw on 2024/7/22.
//

import Foundation
// MARK: - 时间格式
enum TimeFormat:String
{
    case format_default            = "yyyy-MM-dd HH:mm:ss"
    case format_yyMdHm             = "yy-MM-dd HH:mm"
    case format_yyyyMdHm           = "yyyy-MM-dd HH:mm"
    case format_yMd                = "yyyy-MM-dd"
    case format_MdHms              = "MM-dd HH:mm:ss"
    case format_MdHm               = "MM-dd HH:mm"
    case format_Hms                = "HH:mm:ss"
    case format_Hm                 = "HH:mm"
    case format_Md                 = "MM-dd"
    case format_yyMd               = "yy-MM-dd"
    case format_YYMMdd             = "yyyyMMdd"
    case format_yyyyMdHms          = "yyyyMMddHHmmss"
    case format_yyyyMdHmsS         = "yyyy-MM-dd HH:mm:ss.SSS"
    case format_yyyyMMddHHmmssSSS  = "yyyyMMddHHmmssSSS"
    case format_yMdWithSlash       = "yyyy/MM/dd"
    case format_yM                 = "yyyy-MM"
    case format_yMdChangeSeparator = "yyyy.MM.dd"
    case format_dd                 = "dd"
    case format_00                 = "yyyy-MM-dd 00:00:00"
    case format_59                 = "yyyy-MM-dd 23:59:59"
}

extension String {
    //日期 -> 字符串
    func date2String(_ date:Date, dateFormat:TimeFormat = .format_default) -> String {
        let formatter = DateFormatter()
        formatter.locale = Locale.init(identifier: "zh_CN")
        formatter.dateFormat = dateFormat.rawValue
        let date = formatter.string(from: date)
        return date
    }
    
    //字符串 -> 日期
    func string2Date(_ string:String, dateFormat:TimeFormat = .format_default) -> Date {
        let formatter = DateFormatter()
        formatter.locale = Locale.init(identifier: "zh_CN")
        formatter.dateFormat = dateFormat.rawValue
        let date = formatter.date(from: string)
        return date!
    }
    

}
